package com.sansi.pencilbook.service.impl;

import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.BookStore;
import com.sansi.pencilbook.mapper.BookStoreMapper;
import com.sansi.pencilbook.service.BookStoreServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能说明: 书店服务接口
 *
 * @author lsy13
 */
@Service
public class BookStoreServerImpl implements BookStoreServer {

    @Autowired
    private BookStoreMapper bookStoreMapper;

    @Override
    public Result getAllStores() {
        try {
            List<BookStore> list = bookStoreMapper.findAll();
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("查询结果为空");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result getStoreByUser(int userId) {
        try {
            BookStore bookStore = bookStoreMapper.findByUser(userId);
            return bookStore!=null ? Result.ofSuccess("查询成功", 1, bookStore) :
                Result.ofFailed("查询结果为空");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result addStore(BookStore bookStore) {
        try {
            if (bookStoreMapper.findByUser(bookStore.getUserId()) == null) {
                return bookStoreMapper.saveBookStore(bookStore) == 1 ? Result.ofSuccess("注册成功", 1) :
                    Result.ofFailed("重复注册");
            }else {
                return Result.ofFailed("重复注册");
            }
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("注册失败");
        }
    }

    @Override
    public Result getStore(int storeId) {
        try {
            BookStore bookStore = bookStoreMapper.findByStore(storeId);
            return bookStore!=null ? Result.ofSuccess("查询成功", 1, bookStore) :
                Result.ofFailed("查询失败");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("查询失败");
        }
    }

    @Override
    public Result modifyStore(BookStore bookStore) {
        try {
            return bookStoreMapper.updateBookStore(bookStore)==1 ? Result.ofSuccess("修改成功", 1) :
                Result.ofFailed("已经修改成功");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("修改失败");
        }
    }

    @Override
    public Result deleteStore(BookStore bookStore) {
        try {
            return bookStoreMapper.deleteBookStore(bookStore)==1 ? Result.ofSuccess("注销成功", 1) :
                Result.ofFailed("已经注销");
        } catch (Exception throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed("注销失败");
        }
    }
}
